package fractal;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.Image;

import javax.swing.*;
import javax.swing.event.*;

public class JImageDisplay extends JComponent {
	private BufferedImage im;
	
	// ctor
	public JImageDisplay(int width, int height) {
		im = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		super.setPreferredSize(new Dimension(width, height));
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(im, 0, 0, im.getWidth(), im.getHeight(), null);
	}
	
	/**
	 * Clear the image content to make it black
	 */
	public void clearImage() {
		int xlimit = im.getWidth();
		int ylimit = im.getHeight();
		for (int i = 0; i < xlimit; i++) 
			for (int j = 0; j < ylimit; j++)
				im.setRGB(i, j, 0);   // set im to black
		
	}
	
	/**
	 * set the (x,y) to rgbcolor
	 * @param x x cord of image
	 * @param y y cord of image
	 * @param rgbColor RGB color
	 */
	
	public void drawPixel(int x, int y, int rgbColor) {
		int xlimit = im.getWidth();
		int ylimit = im.getHeight();
		if (x < 0 || x >= xlimit || y < 0 || y >= ylimit) 
			throw (new RuntimeException ("illegal x or y"));
		else 
			im.setRGB(x, y, rgbColor);
	}
	
	// gettor 
	public BufferedImage getImage() {
		return im;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
